<?php
  /* @var $configuration \library\configuration\Configruation */

namespace model\database;

/**
 * Description of Database
 *
 * @author Damian Jóźwiak
 */
class Database extends \library\Base{
    /**
     *@readwrite
     * @var type 
     */
    protected $type;
        /**
     *@readwrite
     * @var type 
     */
    protected $options;
    
    /**
     * 
     * @return \model\database\PDOConnector
     * @throws \model\exceptions\ExceptionWrongDatabaseDriver
     * @throws \library\exceptions\ExceptionWrongDriver
     */
    public function initialize(){
        $type = $this->getType();
        if (empty($type)){
          
            $configuration = \library\Registry::get("configuration");
            switch ($_SERVER['HTTP_HOST']){
                case "localhost:8080": {$host ="default"; break;}
                case "alequizdev.pl": {$host="quiz";break;}
            }
            $host="produkcyjna";
            /* @var $configuration \library\configuration\Ini */
           if($configuration){
               $configuration=$configuration->initialize();
               $parsed = $configuration->parse(APP_PATH."/application/configuration/conf");
               if(!empty($parsed->database->$host) && !empty($parsed->database->$host->driverType)){
                   $type = $parsed->database->$host->driverType;
                   unset($parsed->database->$host->driverType);
                   $this->__construct(array(
                       "type"=>$type,
                       "options" =>(array)$parsed->database->$host
                   ));
               }
            }      
       }
        if (!$this->type){
            throw new \model\exceptions\ExceptionWrongDatabaseDriver($this->type);
        }
        switch ($this->type){
            case "PDO":{
                return new PDOConnector($this->options);
            }
            default :{
                throw new \library\exceptions\ExceptionWrongDriver();
            }
        }
    }
}
